package org.xtense.log;

import org.apache.commons.logging.*;
import org.xtense.exception.XtenseException;

public class iLog {
	private static Log logger = LogFactory.getLog("xtense");

	public static void info(Object e) {
		if (e instanceof Exception) {
			if (e instanceof XtenseException) {
				logger.info(((XtenseException) e).getMessage());
			} else {
				logger.info(getException((Exception) e));
			}
		} else {
			logger.info(e.toString());
		}

	}

	public static void warn(Object e) {
		if (e instanceof Exception) {
			if (e instanceof XtenseException) {
				logger.warn(((XtenseException) e).getMessage());
			} else {
				logger.warn(getException((Exception) e));
			}
		} else {
			logger.warn(e.toString());
		}
	}

	public static void debug(Object e) {
		if (e instanceof Exception) {
			if (e instanceof XtenseException) {
				logger.debug(((XtenseException) e).getMessage());
			} else {
				logger.debug(getException((Exception) e));
			}
		} else {
			logger.debug(e.toString());
		}
	}

	public static void error(Object e) {
		if (e instanceof Exception) {
			if (e instanceof XtenseException) {
				logger.error(((XtenseException) e).getMessage());
			} else {
				logger.error(getException((Exception) e));
			}
		} else {
			logger.error(e.toString());
		}
	}

	public static void fatal(Object e) {
		if (e instanceof Exception) {
			if (e instanceof XtenseException) {
				logger.fatal(((XtenseException) e).getMessage());
			} else {
				logger.fatal(getException((Exception) e));
			}
		} else {
			logger.fatal(e.toString());
		}
	}

	public static String getException(Exception e) {
		StackTraceElement[] ste = e.getStackTrace();
		StringBuffer sb = new StringBuffer();
		sb.append(e.getMessage() + " ");
		for (int i = 0; i < ste.length; i++) {
			sb.append(ste[i].toString() + "\n");
		}
		return sb.toString();
	}
}
